#include <iostream>

using namespace std;


struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

ListNode* get_last_k(ListNode* head, int k){
    ListNode* s = head;
    ListNode* f = head;
    for(int i=0;i<k;i++){
        f = f->next;
    }
    while(f){
        f = f->next;
        s = s->next;
    }
    return s;
}


ListNode* removeNthFromEnd(ListNode* head, int n) {
    ListNode* dummy = new ListNode;
    dummy->next = head;
    ListNode* p = get_last_k(dummy, n+1);
    p->next = p->next->next;
    return dummy->next;
}